#include <android/log.h>

extern "C" {

#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavutil/mathematics.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"

}


#include <frame.h>
#include <pthread.h>

//MPEG4 ��Ƶ��֧��AV_PIX_FMT_YUVJ420P�� av_open2ʧ��
#define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */
#define STREAM_FRAME_RATE 10 /* 25 images/s */

class Record
{
public:
	Record();
	~Record();

   bool start(const char* outputFile, int videoWidth, int videoHeight,int fps);
   bool stop();
   void record(unsigned char* nativeRGB,int pts);

   bool WriteVideoFrame(AVFrame* frame, Operation operation, FrameType type);

protected:
   bool open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st);
   bool write_video_frame(AVStream *st, AVFrame *frame);
   void close_video(AVFormatContext *oc, AVStream *st);

   AVStream* add_stream(AVFormatContext *oc, AVCodec **codec, AVCodecID codec_id, AVPixelFormat pixelFormat);

   //bool audioCodecIsSupported(AVCodecContext *audio, AVCodecContext *targetAudio);
private:
   /* audio output */
       uint8_t **src_samples_data;
       int       src_samples_linesize;
       int       src_nb_samples;

       int max_dst_nb_samples;
       uint8_t **dst_samples_data;
       int       dst_samples_linesize;
       int       dst_samples_size;

       void                *_swrBuffer;
       int          _swrBufferSize;


       struct SwrContext *swr_ctx;

       //ת��ʽ
       struct SwsContext* mVideoSwsContext;
       AVFrame *mFrame;
       AVPicture mPicture;

       /* video output */
       //AVFrame *frame;

       AVOutputFormat *fmt;
       AVFormatContext *oc;
       AVStream *video_st;
       AVCodec *video_codec, *audio_codec;
       int frame_count;
       int videoPtsIndex;
       int picWidth;
       int picHeight;

       bool scopeOn;
       bool requireScopeStop;

       AVFrame *m_pRGBFrame;  //RGB帧数据
       AVFrame *m_pYUVFrame;  //YUV帧数据
       int _pkgPtsIndex;
       int _pkgDtsIndex;
       int recordFps;


       AVFormatContext* inputCtx;
       AVStream* input_stream_video;
       void		handleRun(void* ptr);

       float t, tincr, tincr2;
       void initialSampleData(AVCodecContext* c);
};
